import Vue from 'vue'
import Vuex from 'vuex'

//用户登录api
import {
    getToken,
    getRouter
} from '@/api/userApi'

Vue.use(Vuex)

//用户登录信息状态
const userVuex = {
    state: {
        token: sessionStorage.getItem("token"), //用户token值
        permissionRouterList: null, //用户权限列表
        addressCode: sessionStorage.getItem("addressCode"),
        username: sessionStorage.getItem("username"),
        uid: sessionStorage.getItem("uid"),
        currentCode: sessionStorage.getItem("currentCode"),
        adminPermission: sessionStorage.getItem("adminPermission")
    },
    mutations: {
        setToken(state, token) {
            state.token = token
            sessionStorage.setItem("token", token)
        },
        setPermissionRouterList(state, list) {
            state.permissionRouterList = JSON.stringify(list)
        },
        setAddressCode(state, addressCode) {
            state.addressCode = addressCode
            sessionStorage.setItem("addressCode", addressCode)
        }, setUsername(state, username) {
            state.username = username
            sessionStorage.setItem('username', username)
        }, setUid(state, uid) {
            state.uid = uid
            sessionStorage.setItem("uid", uid);
        }, setCurrentCode(state, currentCode) {
            state.currentCode = currentCode
            sessionStorage.setItem("currentCode", currentCode);
        }, setAdminPermission(state, adminPermission) {
            state.adminPermission = adminPermission
            sessionStorage.setItem("adminPermission", adminPermission);
        }
    },
    actions: {
        //获取token
        setToken({
            commit
        }, loginForm) {
            return new Promise((resolve, reject) => {
                getToken(loginForm).then(response => {
                    if (response.data.data.token) {
                        commit('setToken', response.data.data.token)
                        commit('setAddressCode', response.data.data.addressCode)
                        commit('setUsername', response.data.data.username);
                        commit('setUid', response.data.data.uid);
                        commit('setCurrentCode', response.data.data.currentCode);
                        commit('setAdminPermission', response.data.data.adminPermission)
                        // console.log(response.data.data)
                    }
                    resolve(response.data)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        //获取动态router列表
        async getPermissionRouterList({
            commit,
            state
        }) {
            /*  获取后台给的权限数组 */
            let permissionRouterList = []
            return await new Promise((resolve, reject) => {
                getRouter(state.token).then(response => {
                    if (response.data == null || response.data == "") { } else {
                        permissionRouterList = response.data.data
                        commit('setPermissionRouterList', permissionRouterList)
                        resolve(response.data)
                        // console.log(state)
                    }
                }).catch(error => {
                    reject(error)
                })
            })

        }
    },
    getters: {
        token: state => state.token,
        permissionRouterList: state => JSON.parse(state.permissionRouterList),
        addressCode: state => state.addressCode,
        username: state => state.username,
        uid: state => state.uid,
        currentCode: state => state.currentCode,
        adminPermission: state => state.adminPermission
    }
}
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------
//全局组件状态
const componentState = {
    state: {
        isCollapse: false, //左侧菜单栏是否关闭
        breadcrumbList: [], //顶部面包屑数组值
        tagsList: [{
            path: '/index',
            title: '首页',
            icon: 'el-icon-s-home'
        }] //顶部tag数组值
    },
    mutations: {
        setIsCollapse(state, collapse) {
            state.isCollapse = collapse
        },
        setBreadcrumbList(state, list) {
            state.breadcrumbList = list
        },
        addTagsList(state, value) {
            state.tagsList.push(value)
        },
        delTagsList(state, index) {
            state.tagsList.splice(index, 1);
        }
    },
    actions: {
        delTagsList({
            commit,
            state
        }, value) {
            let prevIndex = 0
            for (let i = 0; i < state.tagsList.length; i++) {
                if (state.tagsList[i].path == value) {
                    if (i != 1) {
                        prevIndex = i - 1
                    }
                    commit('delTagsList', i)
                }
            }
            return prevIndex
        }
    },
    getters: {
        isCollapse: state => state.isCollapse,
        breadcrumbList: state => state.breadcrumbList,
        tagsList: state => state.tagsList
    }
}

















export default new Vuex.Store({
    modules: {
        userVuex: userVuex,
        componentState: componentState
    }
})